/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.attach;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.attach.Attachment;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2013-03-07
 */
@Service("attachmentService")
public class AttachmentServiceImpl implements AttachmentService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Attachment
     ****************************************/
    public void insert(Attachment attachment) throws Exception {
        attachment.setFileid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(attachment);
    }

    /****************************************
     * Update a instace of Attachment
     ****************************************/
    public void update(Attachment attachment) throws Exception {
        iDataSet4iBatis.update(attachment);
    }

    /****************************************
     * Delete a instace of Attachment
     ****************************************/
    public void delete(Attachment attachment) throws Exception {
        iDataSet4iBatis.delete(attachment);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(List<BaseModel> attachments) throws Exception {
        iDataSet4iBatis.batchInsert(attachments);
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Attachment attachment) throws Exception {
        String statementId = attachment.getStatmentId();
        String mainId = attachment.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> attachmentList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Attachment model = new Attachment();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            attachmentList.add(model);
        }
        iDataSet4iBatis.batchDelete(attachmentList);
    }

    /****************************************
     * Query instaces of Attachment
     ****************************************/
    public List<BaseModel> queryAsList(Attachment attachment) throws Exception {
        String queryListStatement = "";
        if (attachment.isPaging()) {
            queryListStatement = attachment.getStatmentId();
            attachment.setStatmentId("Attachment.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(attachment);
            attachment.setTotalCount(baseModel.getTotalCount());
            attachment.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(attachment);
    }
    
    /****************************************
     * Query instaces of Attachment
     ****************************************/
    public List<BaseModel> querybyEntityid(AttachLink attachlink) throws Exception {
        attachlink.setStatmentId("Attachment.querybyEntityid");
        return iDataSet4iBatis.queryAsList(attachlink);
    }

    /****************************************
     * Query a instaces of Attachment
     ****************************************/
    public BaseModel queryAsObject(Attachment attachment) throws Exception {
        return iDataSet4iBatis.queryAsObject(attachment);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
